import * as React from "react";
import Model from '../Model/index';
import { connect } from "react-redux";
import {AppToaster} from "../../../../../../component/Toast";
import axios from '../../../../../../config/axios';
import {host} from '../../../../../../config/host';

import './index.less';

class Create extends React.PureComponent {
    constructor(props) {
        super(props);
        this.handleClick = this.handleClick.bind(this);
        this.handleClear = this.handleClear.bind(this);
    }

    componentDidMount() {}

    async handleClick() {
       const model= this.refs.model;
       const {userReduce} = this.props;
       const {org} = userReduce;
       const data = Object.assign({}, model.getValue(), {Org: org});
        try {
            const msg = await axios.post(host + "user/create", data);
            if (msg.data.statusCode === 0) {
                AppToaster.show({ message: msg.data.statusInfo });
            } else {
                AppToaster.show({ message: "添加成功" });
            }
        } catch (e) {
            console.log(e)
            AppToaster.show({ message: "发生错误" });
        }
    }

    handleClear() {
        const model= this.refs.model;
        model.init();
    }

    render() {
        return(
            <div className="create">
                <Model type="create" ref="model" initData={{}}/>
                <a role="button" className="bp3-button .modifier create-btn" onClick={this.handleClick}>确认</a>
                <a role="button" className="bp3-button .modifier create-btn" onClick={this.handleClear}>取消</a>
            </div>
        )
    }
}

const mapStateToProps = ({ homeReduce, userReduce }) => ({ homeReduce, userReduce});
export default connect(mapStateToProps)(Create);